package org.seven.jrdp.biz.sys.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.seven.jrdp.biz.sys.mapper.SysDictMapper;
import org.seven.jrdp.biz.sys.model.SysDict;
import org.seven.jrdp.commons.model.Option;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@CacheConfig(cacheNames = "dict")
@Service
public class SysDictService {
	@Autowired
	private SysDictMapper sysDictMapper;

	@Transactional(readOnly = true)
	public List<SysDict> find(String type) {
		Example example = new Example(SysDict.class);
		example.orderBy("sort");
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("type", type);
		criteria.andEqualTo("deleteFlag", "0");
		return sysDictMapper.selectByExample(example);
	}

	@Transactional(readOnly = true)
	public SysDict get(String id) {
		return sysDictMapper.selectByPrimaryKey(id);
	}

	@CacheEvict(allEntries = true)
	@Transactional
	public void insert(SysDict sysDict) {
		sysDictMapper.insertSelective(sysDict);
	}

	@CacheEvict(allEntries = true)
	@Transactional
	public void update(SysDict sysDict) {
		sysDictMapper.updateByPrimaryKeySelective(sysDict);
	}

	@CacheEvict(allEntries = true)
	@Transactional
	public void delete(String id) {
		SysDict sysDict = new SysDict();
		sysDict.setId(id);
		sysDict.setDeleteFlag("1");
		sysDictMapper.updateByPrimaryKeySelective(sysDict);
	}

	@Cacheable(key = "'dict_option_'+#type")
	@Transactional(readOnly = true)
	public List<Option> findOption(String type) {
		return sysDictMapper.findOption(type);
	}

	@Cacheable(key = "'dict_text_'+#type")
	@Transactional(readOnly = true)
	public List<Option> findText(String type) {
		return sysDictMapper.findText(type);
	}
	
	@Cacheable(key = "'dict_value_'+#type")
	@Transactional(readOnly = true)
	public List<Option> findValue(String type) {
		return sysDictMapper.findValue(type);
	}
	
	@Cacheable(key = "'dict_String_'+#type")
	@Transactional(readOnly = true)
	public List<String> findString(String type) {
		return sysDictMapper.findString(type);
	}
}